package com.peanut.baby.mvp.calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.peanut.baby.R;
import com.peanut.baby.model.Baidai;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.tencent.mars.xlog.Log;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/8/24.
 */

public class BaidaiOptionsListAdapter extends HolderAdapter<Baidai,BaidaiOptionsListAdapter.Holder>{
    private static final String TAG = BaidaiOptionsListAdapter.class.getSimpleName();
    private Baidai checkedItem = null;
    public BaidaiOptionsListAdapter(Context context, List<Baidai> listData) {
        super(context, listData);
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, Baidai baidai, int position) {
        return layoutInflater.inflate(R.layout.layout_item_baidai,null);
    }

    @Override
    public Holder buildHolder(View convertView, Baidai baidai, int position) {
        Holder holder = new Holder();
        holder.root = convertView.findViewById(R.id.baidai_root);
        holder.title = (TextView) convertView.findViewById(R.id.baidai_title);
        holder.desc = (TextView) convertView.findViewById(R.id.baidai_desc);
        holder.checked = convertView.findViewById(R.id.baidai_checkbox);
        return holder;
    }

    @Override
    public void bindViewDatas(Holder holder, final Baidai baidai, int position) {
        holder.title.setText(baidai.title);
        holder.desc.setText(baidai.desc);
        holder.checked.setEnabled(baidai.checked);

        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                baidai.checked = true;
                onCheckedItemChanged(baidai);
            }
        });
    }

    private void onCheckedItemChanged(Baidai checked){
        checkedItem = checked;
        Log.d(TAG,"checked "+checked.title);
        for(int i=0;i<listData.size();i++){
            Log.d(TAG,"listData.get(i) "+listData.get(i).title);
            if(!listData.get(i).title.equals(checked.title)){
                listData.get(i).checked = false;
            }
        }
        notifyDataSetChanged();
    }

    public Baidai getCheckedItem(){
        return checkedItem;
    }


    class Holder{
        View root;
        TextView title;
        TextView desc;
        View checked;
    }
}
